/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.proxy;

import com.jspx.json.JSONObject;
import com.jspx.txweb.ActionProxy;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Operate;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.ClassUtil;
import com.jspx.utils.StringUtil;
import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2006-12-26
 * Time: 23:59:57
 * action 代理执行容器
 */
public class DefaultActionProxy implements ActionProxy {
    private String method = TXWebUtil.defaultExecute;
    private ActionSupport action;

    public DefaultActionProxy()  {

    }

    public void setCallJson(JSONObject callJson) {
        action.put(ActionSupport.Key_CallRocJsonData,callJson);
    }

    public JSONObject getCallJson() {
        return  (JSONObject)action.getEnv().get(ActionSupport.Key_CallRocJsonData);
    }



    public void destroy() {

        action.destroy();
    }

    public ActionSupport getAction() {
        return action;
    }

    public void setAction(ActionSupport action) {
        this.action = action;
    }

    public String getMethod() {
        if (StringUtil.isNULL(method)) return TXWebUtil.defaultExecute;
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getActionName() {
        return action.getEnv(ActionSupport.Key_ActionName);
    }

    public String getNamespace() {
        return action.getEnv(ActionSupport.Key_Namespace);
    }

    public String getCaption() {
        HttpMethod httpMethod = action.getClass().getAnnotation(HttpMethod.class);
        if (httpMethod != null) return httpMethod.caption();
        return StringUtil.empty;
    }

    public String getMethodCaption() {
        try
        {
            Method exeMethod = ClassUtil.getDeclaredMethod(action.getClass(), method);
            if (exeMethod == null) return StringUtil.empty;
            Operate operate = exeMethod.getAnnotation(Operate.class);
            if (operate != null) return operate.caption();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return StringUtil.empty;
    }

    /**
     * 执行action方法
     *
     * @return action 返回
     * @throws Exception 异常
     */
    public String execute() throws Exception {
        action.initialize();
        if (StringUtil.isNULL(method)) method = TXWebUtil.defaultExecute;
        TXWebUtil.setTurnPage(action);
        //执行配置方法
        JSONObject callJson = getCallJson();
        if (callJson!=null&&!callJson.isEmpty())
        {
            TXWebUtil.invokeJson(this);
            //执行默认的Execute方法begin
            TXWebUtil.invokeExecute(action, TXWebUtil.defaultExecute,TXWebUtil.jsonType);
            //执行默认的Execute方法end
            return ActionSupport.ROC;
        }
        //检测设置运行翻页程序 标签
        if (!TXWebUtil.defaultExecute.equals(method) && !ActionSupport.NONE.equals(method)) {
            if (ActionSupport.KEY_RETURN.equals(TXWebUtil.invokeExecute(action, method,TXWebUtil.htmlType)))
                return ActionSupport.KEY_RETURN;
        }
        //最后执行返回,如果没有返回这里，一般就是拦截器拦截了，检查权限判断部分
        return TXWebUtil.invokeExecute(action, TXWebUtil.defaultExecute,TXWebUtil.htmlType);
    }
}